<?php
	require_once $_SERVER['DOCUMENT_ROOT'] .
		'/SAGER/model/interfaces/INaturalCustomerAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IConnectionAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/ConnectionFactory.php';
	
	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];

	class MySqlNaturalCustomerAccessDao implements INaturalCustomerAccessDao {
  		/**
		 * Constructor of the natural customer dao
		 */
		public function __construct () {}
		
		/**
		 * Creates a natural customer
		 */
		public function createNaturalCustomer($naturalCustomer) {
			$pkIdNumber = $naturalCustomer -> getPkIdNumber();
			$firstName = $naturalCustomer -> getFirstName();
			$secondName = $naturalCustomer -> getSecondName();
			$firstLastName = $naturalCustomer -> getFirstLastName();
			$secondLastName = $naturalCustomer -> getSecondLastName();
			$emailAddress = $naturalCustomer -> getEmailAddress();		
			
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			$query = "INSERT INTO NATURAL_CUSTOMER VALUES ($pkIdNumber, '$firstName', 
				'$secondName', '$firstLastName', '$secondLastName', '$emailAddress')";
			
			$sentence = mysql_query($query, $conn);
			
			if ($sentence)
				return 'true';
			
			return mysql_errno($conn);
		}
		
		public function changeNaturalCustomer($naturalCustomer) {
		   
			$connection = ConnectionFactory::getMySqlConnectionAccessDao();
			$conn=$connection->openConnection();
			$conn = mysql_connect("localhost", "root", "");
			mysql_select_db("sager", $conn);	
			 
			$query = "UPDATE EMPLOYEE SET $pk_id_number=$pk_id_number, $first_name='$first_name', $second_name='$second_name', $first_last_name='$first_last_name',$second_last_name='$second_last_name', $email_address='$email_address'";
		 
		 
		 
			$sentence=mysql_query($query,$conn);
			   if ($sentence){
					return true;
				   }
			  return false;
		 
		 }
		 
		/**
		 * Selects a natural customer by id number
		 */
		public function selectNaturalCustomerByIdNumber($ci) { 
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			$query = "SELECT first_name, second_name, first_last_name, second_last_name, 
						  email_address 
					  FROM NATURAL_CUSTOMER 
					  WHERE pk_id_number = $ci";
					  
			$GLOBALS['debug'] -> log($query, "Query");
			
			$sentence = mysql_query($query, $conn);
			
			$conn = $connection -> closeConnection($conn);
			
			return $sentence;
		}
	}
?>